package controller;

import implementor.MaterialImp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import daoInterface.MaterialDao;
import dto.Material;

import views.inventory.ManuallyStockUpdateView;

public class ManuallyStockUpdateController implements ActionListener 
{
	private ManuallyStockUpdateView view;
	private Material m;
	
	
	
	public ManuallyStockUpdateController(ManuallyStockUpdateView view) 
	{
		super();
		this.view = view;
		
		view.addActionListenerOnButtons(this);
	}



	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String event=arg0.getActionCommand();
		
		if(event.contains("update"))
		{
			String matcode=view.getTfMaterialCode().getText();
			
			MaterialDao md=new MaterialImp();
			try 
			{
				m=md.retrieve("mCode", matcode);
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			
			m.setmQuant(Double.parseDouble(view.getTfQuantity().toString()));
			JOptionPane.showMessageDialog(null, "Material is updated successfully");
		}
		
		if(event.contains("increase"))
		{
			view.getTfQuantity().setText(Integer.parseInt(view.getTfQuantity().getText())+1+"");
		}
		
		if(event.contains("decrease"))
		{
			view.getTfQuantity().setText(Integer.parseInt(view.getTfQuantity().getText())-1+"");
		}
	}

}
